/*
*** Modified 09/19/2011 to return blips per second
 
 ***  countBlips - measures the time taken to move some 
 number of tachometer "blips"
 
 Modified for two motors
 */

int ledPin11 = 11;                      // Motor1 LED (pin 11)
int ledPin12 = 12;                      // Motor2 LED (pin 12) 

volatile unsigned long blipCounter1 = 0;  // Counter for number of blips seen on Tach1 (from motor1)
volatile unsigned long blipCounter2 = 0;  // Counter for number of blips seen on Tach2 (from motor2)

int flashLEDs = HIGH;

unsigned long current_time = 0;    // Variable to store current time in microseconds
unsigned long last_time    = 0;       // Variable to store last time in microseconds

unsigned long blips_from_t1 = 0;    // Var to store blipCounter1's value every time_increment microseconds
unsigned long blips_from_t2 = 0;    // Var to store blipCounter2's value every time_increment microseconds

String totalBlips;      // String to print to serial line

unsigned long time_increment = 500000;               // set time to increment by (milliseconds) 
// 500 milliseconds is .5 seconds and 500 000 microseconds
char outputBuffer[256];

void setup()
{
  pinMode(ledPin11, OUTPUT);              // Configure motor1 LED pin
  pinMode(ledPin12, OUTPUT);              // Configure motor2 LED pin
  digitalWrite(2, HIGH);                 // Set pullup resistor on external interrupt pin 2 (data from Tach1)
  digitalWrite(3, HIGH);                 // Set pullup resistor on external interrupt pin 3 (data from Tach2)

  attachInterrupt(0, countBlips1, RISING);  // Attach Interrupt Routine to External Interrupt 0, on pin 2, to trigger an interrupt on signal's rising edge (Tach1)
  attachInterrupt(1, countBlips2, RISING);  // Attach Interrupt Routine to External Interrupt 1, on pin 3, to trigger an interrupt on signal's rising edge (Tach2)

  Serial.begin(9600);                    // Start serial connection

}

void loop()
{ 
  current_time = micros();    // Snag current time in microseconds

    if( (current_time - last_time) >= time_increment ){
    blips_from_t1 = blipCounter1;
    blips_from_t2 = blipCounter2;    // snag current num counts
    blipCounter1 = 0;
    blipCounter2 = 0;                // reset counters

    totalBlips = (String)blips_from_t1;
    totalBlips = totalBlips.concat(" ");
    totalBlips = totalBlips.concat((String)blips_from_t2);
    totalBlips = totalBlips.concat(" ");
    totalBlips = totalBlips.concat((String)(current_time - last_time));

    Serial.println(totalBlips);
    // Output is in the form 'xxx yyy zzzzzz'
    // where x is the number of blips from tach1, y is the number of
    // blips from tach 2, and z is the time increment in microseconds
    // during which these measurements were taking. Blips per second for each motor

    digitalWrite(ledPin11, flashLEDs);   // LEDs toggle state wgen time_increment was reached
    digitalWrite(ledPin12, flashLEDs);   // these aren't working for some reason, although
    // the onboard led (13?) flashes sometimes
    flashLEDs = ! flashLEDs;  

    last_time = current_time;
    // Oscillator is 16 MHz
    // So ever 1/16e6 seconds, a command is executed.
    // ie, every 6.25 * 10^-8 = .0625 microseconds
  } 
}

void countBlips1()          // Routine to run upon recieving interrupt on pin 2 (from Tach1)
{
  blipCounter1++;           // Just increment the blip counter1 
}

void countBlips2()          // Routine to run upon recieving interrupt on pin 3 (from Tach2)
{
  blipCounter2++;           // Just increment the blip counter 
}

